package cn.itcast.tanhua.controller;

import cn.itcast.model.UserInfo;
import cn.itcast.tanhua.interceptor.UserHolder;
import cn.itcast.tanhua.server.UserInfoService;
import cn.itcast.tanhua.server.UserService;
import cn.itcast.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.ibatis.annotations.Lang;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private UserService userService;

    /**
     * 用户登录-发送验证码
     *
     * @param param
     * @return
     */
    @PostMapping("login")
    public ResponseEntity login(@RequestBody Map<String, String> param) {
        //1. 获取手机号
        String phone = param.get("phone");
        //2. 发送验证码
        userService.sendCode(phone);
        //3. 返回数据
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
        //1 调用map集合获取请求参数
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        //2 调用userService完成用户登录
        Map retMap = userService.loginVerification(phone, code);
        //3 构造返回
        return ResponseEntity.ok(retMap);
    }
    /**
     * 保存用户信息
     *   UserInfo
     *   请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //1. 校验token
        Long userId = UserHolder.getUserId();
        userInfo.setId(userId);
        //调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);

    }

    /**
     * 头像
     * @param headPhoto
     * @param token
     * @return
     * @throws IOException
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity head(MultipartFile headPhoto,
                               @RequestHeader("Authorization") String token) throws IOException {

        boolean verifyToken = JwtUtils.verifyToken(token);
        if (!verifyToken){
            return ResponseEntity.status(401).body(null);
        }
        //2、向userinfo中设置用户id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //调用service
        userInfoService.updateHead(headPhoto,Long.valueOf(id));
        return ResponseEntity.ok(null);
    }

    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo,
                                         @RequestHeader("Authorization") String token){
        //1、判断token是否合法
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2、获取id
        Claims claims = JwtUtils.getClaims(token);
        Integer id = (Integer) claims.get("id");
        //设置id
        userInfo.setId(Long.valueOf(id));
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }

}